package service

import (
	sqlc "bgs/db/sqlc"
	"bgs/util"
	"context"
	"encoding/json"
	"fmt"
	"time"
)

type loggingAccountForOrderDistributorCommissionProfitExt struct {
	DistributorID             int64     `json:"distributor_id"`
	DistributorUserID         int64     `json:"distributor_user_id"`
	OrderSerialNumber         string    `json:"order_serial_number"`
	LedgerRequestSerialNumber string    `json:"ledger_request_serial_number"`
	Finished                  time.Time `json:"finished"`
	WxLedgerOrderNo           string    `json:"wx_ledger_order_no"`
	Scene                     string    `json:"scene"`
}

// LoggingAccountForOrderDistributorCommissionProfit 分销门店分销订单佣金记账
func (s *StoreServiceImp) LoggingAccountForOrderDistributorCommissionProfit(ctx context.Context, q *sqlc.Queries, ledgerRequestPtr *sqlc.LedgerRequest, orderPtr *sqlc.Order, wxLedgerOrderNo string, occurred time.Time) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("==begin 分销门店分销订单佣金记账")

	orderSerialNumber := ledgerRequestPtr.OrderSerialNumber
	ledgerRequestSerialNumber := ledgerRequestPtr.SerialNumber
	cBrand := s.cacher.FetchBrand(ledgerRequestPtr.BrandID)
	if cBrand == nil {
		err = fmt.Errorf("not found brand of brandID:%d", ledgerRequestPtr.BrandID)
		return
	}
	slog.Infof("2、获取门店分销订单品牌缓存信息 ok")

	storeID := orderPtr.StoreID.Int64
	distributorID := orderPtr.DistributorID.Int64
	distributorUserID := orderPtr.DistributorUserID.Int64

	extInfoBytes, marshalErr := json.Marshal(&loggingAccountForOrderDistributorCommissionProfitExt{
		DistributorID:             distributorID,
		DistributorUserID:         distributorUserID,
		OrderSerialNumber:         orderSerialNumber,
		LedgerRequestSerialNumber: ledgerRequestSerialNumber,
		Finished:                  occurred,
		WxLedgerOrderNo:           wxLedgerOrderNo,
		Scene:                     "分销订单佣金",
	})
	if marshalErr != nil {
		err = marshalErr
		return
	}

	loggingParam := LoggingAccountParam{
		StoreID:  storeID,
		BrandID:  ledgerRequestPtr.BrandID,
		IsIn:     true,
		Category: sqlc.EmStoreAccountLogCategoryDistributorCommission,
		Amount:   ledgerRequestPtr.DistributorCommission,
		Subject:  fmt.Sprintf("分销订单佣金-%s", cBrand.Name),
		ExtInfo:  string(extInfoBytes),
	}

	err = s.loggingAccount(ctx, q, loggingParam)
	if err != nil {
		return
	}
	slog.Infof("3 门店账户变动日志(明细账)分销门店分销订单佣金记账 ok")

	slog.Infof("==end 分销门店分销订单佣金记账 ok")
	return
}

type loggingAccountForOrderDistributorCommissionProfitRollbackExt struct {
	DistributorID              int64     `json:"distributor_id"`
	DistributorUserID          int64     `json:"distributor_user_id"`
	OrderSerialNumber          string    `json:"order_serial_number"`
	AfterSaleSerialNumber      string    `json:"after_sale_serial_number"`
	LedgerRollbackSerialNumber string    `json:"ledger_rollback_serial_number"`
	Finished                   time.Time `json:"finished"`
	WxLedgerReturnNo           string    `json:"wx_ledger_return_no"`
	Scene                      string    `json:"scene"`
}

// LoggingAccountForOrderDistributorCommissionProfitRollback 分销门店分销订单回退记账
func (s *StoreServiceImp) LoggingAccountForOrderDistributorCommissionProfitRollback(ctx context.Context, q *sqlc.Queries, ledgerRollbackPtr *sqlc.LedgerRollback, orderPtr *sqlc.Order, wxLedgerReturnNo string, occurred time.Time) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("==begin 分销门店分销订单回退记账")

	cBrand := s.cacher.FetchBrand(ledgerRollbackPtr.BrandID)
	if cBrand == nil {
		err = fmt.Errorf("not found brand of brandID:%d", ledgerRollbackPtr.BrandID)
		return
	}
	slog.Infof("1、获取分销订单品牌缓存信息 ok")

	storeID := orderPtr.StoreID.Int64
	distributorID := orderPtr.DistributorID.Int64
	distributorUserID := orderPtr.DistributorUserID.Int64
	orderSerialNumber := ledgerRollbackPtr.OrderSerialNumber
	afterSaleSerialNumber := ledgerRollbackPtr.AfterSaleSerialNumber
	ledgerRollbackSerialNumber := ledgerRollbackPtr.SerialNumber

	extInfoBytes, marshalErr := json.Marshal(&loggingAccountForOrderDistributorCommissionProfitRollbackExt{
		DistributorID:              distributorID,
		DistributorUserID:          distributorUserID,
		OrderSerialNumber:          orderSerialNumber,
		AfterSaleSerialNumber:      afterSaleSerialNumber,
		LedgerRollbackSerialNumber: ledgerRollbackSerialNumber,
		Finished:                   occurred,
		WxLedgerReturnNo:           wxLedgerReturnNo,
		Scene:                      "分销订单佣金回退",
	})
	if marshalErr != nil {
		err = marshalErr
		return
	}

	loggingParam := LoggingAccountParam{
		StoreID:  storeID,
		BrandID:  ledgerRollbackPtr.BrandID,
		IsIn:     false,
		Category: sqlc.EmStoreAccountLogCategoryRollbackDistributorCommission,
		Amount:   ledgerRollbackPtr.DistributorCommission,
		Subject:  fmt.Sprintf("分销订单佣金回退-%s", cBrand.Name),
		ExtInfo:  string(extInfoBytes),
	}

	err = s.loggingAccount(ctx, q, loggingParam)
	if err != nil {
		return err
	}
	slog.Infof("3 门店账户变动日志(明细账)分销门店分销订单回退 ok")

	slog.Infof("==end 分销门店分销订单回退记账 ok")
	return
}
